package com.mframe.struts;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mframe.query.PageData;
import com.mframe.system.LoginInfo;
import com.mframe.system.SystemManager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * @author cuijr
 * @creation 2013-2-3
 */
public class PublicStrutsAction extends ActionSupport implements Preparable {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    protected Logger logger;
    protected final HttpServletRequest request;
    protected final HttpServletResponse response;
    protected final HttpSession session;
    public static final String RELOAD = "reload";
    public static final String VIEW = "view";
    public static final String FUNCTION_ADD = "ADD";
    public static final String FUNCTION_EDIT = "EDIT";
    public static final String FUNCTION_VIEW = "VIEW";
    public static final String FUNCTION_AUDIT = "AUDIT";
    protected String recordId;
    protected String func;
    /**
     * 单据编号
     */
    protected Long operSerialNo;
    protected Integer currentPage;
    /**
     * 条件
     */
    protected Map<String, String> conditionMap;
    protected PageData pageData;
    protected List<String> scriptMessages;

    public PublicStrutsAction() {
	this.logger = LoggerFactory.getLogger(getClass());
	this.request = ServletActionContext.getRequest();
	this.response = ServletActionContext.getResponse();
	this.session = this.request.getSession();

	this.currentPage = Integer.valueOf(1);
	this.conditionMap = new HashMap();
	this.scriptMessages = new ArrayList();
    }

    @Override
    public void prepare() throws Exception {

    }

    protected void addScriptMessage(String msg) {
	this.scriptMessages.add(msg);
	this.request.removeAttribute("actionScriptMessages");
	this.request.setAttribute("actionScriptMessages", this.scriptMessages);
    }

    protected void cleanScriptMessage() {
	this.scriptMessages.clear();
	this.request.removeAttribute("actionScriptMessages");
    }

    protected void setPageEditable(boolean editAble) {
	addScriptMessage("$(function(){setPageUnEdit();});");
    }

    protected void setPageElementsEditableById(boolean editAble,
	    String[] controllerId) {
	StringBuffer str = new StringBuffer("");
	for (String id : controllerId) {
	    str.append(",").append(id);
	}
	String idStr = str.substring(1);
	idStr = "'" + idStr + "'";
	addScriptMessage("$(function(){setPageElementsUnEdit(" + idStr + ","
		+ editAble + ");});");
    }

    /**
     * @return loginInfo 对象,主要是从action基类获取
     */
    public LoginInfo getLoginInfo() {
	return SystemManager.getLoginInfo();
    }

    public Date getCurrentTime() {
	return new Date();
    }

    public String getRecordId() {
	return this.recordId;
    }

    public void setRecordId(String recordId) {
	this.recordId = recordId;
    }

    public Long getOperSerialNo() {
	return this.operSerialNo;
    }

    public void setOperSerialNo(Long operSerialNo) {
	this.operSerialNo = operSerialNo;
    }

    public Integer getCurrentPage() {
	return this.currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
	this.currentPage = currentPage;
    }

    public String getFunc() {
	return this.func;
    }

    public void setFunc(String func) {
	this.func = func;
    }

    public Map<String, String> getConditionMap() {
	return this.conditionMap;
    }

    public void setConditionMap(Map<String, String> conditionMap) {
	this.conditionMap = conditionMap;
    }

    public PageData getPageData() {
	return this.pageData;
    }

    public void setPageData(PageData pageData) {
	this.pageData = pageData;
    }

    public List<String> getScriptMessages() {
	return this.scriptMessages;
    }

    public void setScriptMessages(List<String> scriptMessages) {
	this.scriptMessages = scriptMessages;
    }
}
